/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panier.gestionnaires;

import panier.modeles.Panier;
import dvd.modeles.Dvd;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Marcouss
 */
@Stateless
@LocalBean
public class GestionnairePanier {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em; 
    
    public void creerDvdTest(){
        System.out.println("testing dvdss");
    }
    
    public Panier addPanier(String login,int idDvd){
        Panier p = new Panier(login,idDvd);
        em.persist(p);
        return p;
    }
    public void removeFromPanier(String login,int idDvd){
        Query q = em.createQuery("DELETE FROM Panier p WHERE p.login = '"+login+"' AND p.idDvd='"+idDvd+"'");
        //"UPDATE Employee e SET e.salary = e.salary + 100 WHERE e.name = :name");
        q.executeUpdate();
    }
    
    public Collection<Panier> getMyPanier(String login) {  
        // Exécution d'une requête équivalente à un select * 
        System.out.println("i'm in get my panier");
        Query q = em.createQuery("select d from Dvd d where d.idDvd IN (select p.idDvd from Panier p where p.login='" + login +"')");  
        return q.getResultList();  
    }
}
